//
//  AUWaveView.swift
//  TestVoiceWave
//
//  Created by yu on 2025/4/10.
//

/*
 ① 两个 aView, bView , 设置颜色, 绘制path , 使用mark, 来透视
 ② 直接使用两个 layer, 绘制显示
 ③ 直接使用View 的draw 方法绘制两条曲线, 刷新view
 */

import Foundation
import UIKit

class AUWaveView: UIView {
    
    //    lazy var firstLevelView = UIView()
    //    lazy var secondLevelView = UIView()
    // 声音大小
    var amplitude: Double = 0 {
        didSet {
            // print("amplitude = \(amplitude)")
            updateWavePaths()
        }
    }
    private var phase: CGFloat = 0.0
    
    private var firstShapeLayer =  CAShapeLayer()
    private var secondShapeLayer = CAShapeLayer()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .clear
        setShapeLyaer(firstShapeLayer, color: .hexStringColor(hexString: "#0089EA"), lineWidth: 2)
        setShapeLyaer(secondShapeLayer, color: .hexStringColor(hexString: "#0089EA"), lineWidth: 1)
        
        layer.addSublayer(secondShapeLayer)
        layer.addSublayer(firstShapeLayer)
        
        //        firstLevelView.backgroundColor = UIColor.red
        //        addSubview(secondLevelView)
        //        
        //        secondLevelView.backgroundColor = UIColor.red
        //        addSubview(firstLevelView)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //        firstShapeLayer.frame = bounds
        //        secondShapeLayer.frame = bounds
    }
    
    //    private func setupLayers() {
    //        
    //    }
    
    private func setShapeLyaer(_ layer: CAShapeLayer, color: UIColor, lineWidth: CGFloat) {
        layer.strokeColor = color.cgColor
        layer.fillColor = UIColor.clear.cgColor
        layer.lineWidth = lineWidth
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateWavePaths() {
        
        let topPath = UIBezierPath()
        let bottomPath = UIBezierPath()
        let width = bounds.width
        let midHeight = bounds.height / 2
        
        topPath.move(to: CGPoint(x: 0, y: midHeight))
        bottomPath.move(to: CGPoint(x: 0, y: midHeight))
        
        for x in stride(from: 0, to: width, by: 1) {
            let relativeX = x / width * 2
            let y =  midHeight * amplitude * sin(2 * .pi * relativeX + phase)
            topPath.addLine(to: CGPoint(x: x, y: midHeight - y))
            bottomPath.addLine(to: CGPoint(x: x, y: midHeight + y))
            //            print("relativeX = \(relativeX) y = \(y)")
        }
        
        firstShapeLayer.path = topPath.cgPath
        secondShapeLayer.path = bottomPath.cgPath
    }
    
    //    override func draw(_ rect: CGRect) {
    //        super.draw(rect)
    //    }
    
    
    //    func generateShaperLayer(lineWidth: CGFloat) -> CAShapeLayer {
    //        let waveLine = CAShapeLayer()
    //        waveLine.lineCap = .butt ; // 线条端点样式（直角）
    //        waveLine.lineJoin = .round; // 线条连接样式（圆角）
    //        waveLine.strokeColor = UIColor.red.cgColor; // 线条颜色
    //        waveLine.fillColor = UIColor.clear.cgColor; // 填充颜色（透明）
    //        waveLine.lineWidth = lineWidth; // 线条宽度
    //        waveLine.backgroundColor = UIColor.clear.cgColor; // 背景颜色（透明）
    //        waveLine
    //    }
    
    
    //    func shpLayer
    //    
    //    // 生成形状层（设置线条样式）
    //    -(CAShapeLayer*)generateShaperLayerWithLineWidth:(CGFloat)lineWidth {
    //        CAShapeLayer* waveLine = [CAShapeLayer layer];
    //        waveLine.lineCap = kCALineCapButt; // 线条端点样式（直角）
    //        waveLine.lineJoin = kCALineJoinRound; // 线条连接样式（圆角）
    //        waveLine.strokeColor = [UIColor redColor].CGColor; // 线条颜色
    //        waveLine.fillColor = [UIColor clearColor].CGColor; // 填充颜色（透明）
    //        waveLine.lineWidth = lineWidth; // 线条宽度
    //        waveLine.backgroundColor = [UIColor clearColor].CGColor; // 背景颜色（透明）
    //        return
    //    }
    
}
